package com.coder.bike.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.BikeBrand;
import com.coder.bike.service.IBikeBrandService;
import com.coder.bike.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/bike/bikeBrand")
public class BikeBrandController {
    @Resource
    private IBikeBrandService bikeBrandService;


    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody BikeBrand BikeBrand) {
        Page<BikeBrand> page = new Page<>(start, size);
        return Result.success()
                .setData(bikeBrandService.searchByPage(page, BikeBrand));
    }

    @PostMapping
    public Result save(@RequestBody BikeBrand bikeBrand) {
        return bikeBrandService.save(bikeBrand) ?
                Result.success() : Result.fail();
    }

    @PutMapping
    public Result update(@RequestBody BikeBrand BikeBrand) {
        return bikeBrandService.updateById(BikeBrand) ?
                Result.success() : Result.fail();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        return bikeBrandService.removeByIds(list) ?
                Result.success() : Result.fail();
    }

    @GetMapping
    public Result selectAll() {
        return Result.success()
                .setData(bikeBrandService.list());
    }

    @GetMapping("/selectByMakerId/{makeId}")
    public Result selectByMakerId(@PathVariable Integer makeId) {
        QueryWrapper<BikeBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mid", makeId);
        return Result.success().setData(bikeBrandService.list(queryWrapper));
    }
}
